package cn.jason.org.controller;

import cn.jason.basic.util.AjaxResult;
import cn.jason.basic.util.PageList;
import cn.jason.org.domain.Dictionary;
import cn.jason.org.query.DictionaryQuery;
import cn.jason.org.service.IDictionaryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author xxx
 */
@RestController
@RequestMapping("/dictionary")
@Api(value = "字典的API",description="字典相关的CRUD功能")
public class DictionaryController {

    private final IDictionaryService dictionaryService;

    public DictionaryController(IDictionaryService dictionaryService) {
        this.dictionaryService = dictionaryService;
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "通过ID查询" )
    public Dictionary queryById(@PathVariable("id") Long id){

        return dictionaryService.queryById(id);
    }

    /**
     * 查询所有
     * @return List<Dictionary>
     */
    @GetMapping
    public List<Dictionary> queryAll(){
        return dictionaryService.queryAll();
    }

    /**
     * 高级查询
     * @param query 查询对象
     * @return PageList<Dictionary>
     */
    @PostMapping
    public PageList<Dictionary> queryPage(@RequestBody DictionaryQuery query){
        return dictionaryService.queryPage(query);
    }

    /**
     * 新增or修改
     * @param dictionary  实体对象
     * @return AjaxResult
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Dictionary dictionary){
        try {
            if(dictionary.getId() == null){
                dictionaryService.save(dictionary);
            }else{
                dictionaryService.update(dictionary);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("正在准备开服前的测试！！");
        }
    }

    /**
     * 删除
     * @param id id
     * @return AjaxResult
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id){
        try {
            dictionaryService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("正在准备开服前的测试！！");
        }
    }
    @PatchMapping
    public AjaxResult batchRemove(@RequestBody List<Long> ids){
        try {
            dictionaryService.batchRemove(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult
                    .me()
                    .setSuccess(false)
                    .setMessage("正在准备开服前的测试！！");
        }
    }


}
